/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhu.profiteer.data;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.EntityNotFoundException;
import jhu.profiteer.data.exceptions.NonexistentEntityException;

/**
 *
 * @author Jeremy
 */
public class CommodityJpaController {

  public CommodityJpaController() {
    emf = Persistence.createEntityManagerFactory("profiteer-gamePU");
  }
  private EntityManagerFactory emf = null;

  public EntityManager getEntityManager() {
    return emf.createEntityManager();
  }

  public void create(Commodity commodity) {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      em.persist(commodity);
      em.getTransaction().commit();
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void edit(Commodity commodity)
          throws NonexistentEntityException, Exception {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      commodity = em.merge(commodity);
      em.getTransaction().commit();
    } catch (Exception ex) {
      String msg = ex.getLocalizedMessage();
      if (msg == null || msg.length() == 0) {
        Long id = commodity.getId();
        if (findCommodity(id) == null) {
          throw new NonexistentEntityException("The commodity with id " + id +
                  " no longer exists.");
        }
      }
      throw ex;
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void destroy(Long id) throws NonexistentEntityException {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      Commodity commodity;
      try {
        commodity = em.getReference(Commodity.class, id);
        commodity.getId();
      } catch (EntityNotFoundException enfe) {
        throw new NonexistentEntityException("The commodity with id " + id +
                " no longer exists.", enfe);
      }
      em.remove(commodity);
      em.getTransaction().commit();
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public List<Commodity> findCommodityEntities() {
    EntityManager em = getEntityManager();
    try {
      return em.createQuery("select object(o) from Commodity as o")
              .getResultList();
    } finally {
      em.close();
    }
  }

  public Commodity findCommodity(Long id) {
    EntityManager em = getEntityManager();
    try {
      return em.find(Commodity.class, id);
    } finally {
      em.close();
    }
  }

  public boolean testFindCommodity(Long id)
  {
    EntityManager em = getEntityManager();
    try {
      List<Commodity> l = (List<Commodity>)em.createQuery("select object(o)" +
              " from Commodity o where o.id = :id")
              .setParameter("id", id)
              .getResultList();
      if(1 == l.size()) return true;
      else return false;
    } finally {
      em.close();
    }
  }
}
